import React, {Component} from 'react';

class App extends Component {
    state = {
        currentIndex: 0
    }
    add = () => {
        // setState是异步更新状态 所以这里同步增加3次实际只算1次
        this.setState({
            currentIndex: this.state.currentIndex + 1
        })
        this.setState({
            currentIndex: this.state.currentIndex + 1
        })
        this.setState({
            currentIndex: this.state.currentIndex + 1
        })
    }

    add2 = () => {
        this.setState({
            currentIndex: this.state.currentIndex + 1
        }, () => {
            this.setState({
                currentIndex: this.state.currentIndex + 1
            }, () => {
                this.setState({
                    currentIndex: this.state.currentIndex + 1
                })
            })
        })
    }

    render() {
        return (<div>
            {this.state.currentIndex}
            <button onClick={this.add}>+</button>
            <button onClick={this.add2}>+</button>
        </div>);
    }
}

export default App;